#include "wa_uart_drv.h"
#include "bsp_uart.h"
#include "wa_misc.h"
#include "wa_input.h"
#include "zfifo.h"

#define WA_UART_DRV_FIFO_SIZE		16

zfifo_t wa_uart_drv_fifo;
uint8_t wa_uart_drv_fifo_buf[WA_UART_DRV_FIFO_SIZE];

void wa_uart_rx_irq_callback(uint8_t byte)
{
	zfifo_put(&wa_uart_drv_fifo, (void*)&byte);
}

void wa_uart_drv_init(void)
{
	zfifo_init(&wa_uart_drv_fifo, wa_uart_drv_fifo_buf, WA_UART_DRV_FIFO_SIZE, sizeof(uint8_t));
	
	bsp_uart_init(BSP_UART8, 115200);
	bsp_uart_irq_rx_enable(BSP_UART8, wa_uart_rx_irq_callback);
}

void wa_uart_drv_loop(void)
{
	uint8_t temp;
	if(zfifo_get(&wa_uart_drv_fifo, (void*)&temp) == 0)
	{
		if(temp == WA_SIGNAL_USER_KEY_UP)
		{
			wa_input_send_sign(WA_SIGNAL_USER_KEY_UP);
		}
		else if(temp == WA_SIGNAL_USER_KEY_DOWN)
		{
			wa_input_send_sign(WA_SIGNAL_USER_KEY_DOWN);
		}
		else if(temp == WA_SIGNAL_USER_KEY_LEFT)
		{
			wa_input_send_sign(WA_SIGNAL_USER_KEY_LEFT);
		}
		else if(temp == WA_SIGNAL_USER_KEY_RIGHT)
		{
			wa_input_send_sign(WA_SIGNAL_USER_KEY_RIGHT);
		}
		else if(temp == WA_SIGNAL_USER_KEY_ENTER)
		{
			wa_input_send_sign(WA_SIGNAL_USER_KEY_ENTER);
		}
		else if(temp == WA_SIGNAL_USER_KEY_ESC)
		{
			wa_input_send_sign(WA_SIGNAL_USER_KEY_ESC);
		}
	}
}
